package com.lanhai.mind.impl;

import com.lanhai.mind.beans.AgvMountBean;
import com.lanhai.mind.beans.DeviceBean;
import com.lanhai.mind.beans.DeviceMountBean;
import com.lanhai.mind.dao.DeviceDao;
import com.lanhai.mind.enums.AgvCurrentTransEnum;
import com.lanhai.mind.enums.ApplyTransferEnum;
import com.lanhai.mind.enums.TransDirectionEnum;
import com.lanhai.mind.interfaces.IAgv;
import com.lanhai.mind.interfaces.IAgvMount;
import com.lanhai.mind.interfaces.IDevice;
import com.lanhai.mind.interfaces.IDeviceMount;
import com.lanhai.mind.interfaces.IDeviceProtocol;
import com.lanhai.mind.interfaces.ITempData;
import com.lanhai.mind.utils.DataSendTools;

/**
 * 小车的装货情况
 * agvmount_{agvid}
 * @author zhengwei
 *
 */
public class AgvMountImpl implements IAgvMount {
	IDevice iDevice;
	IDeviceMount iDeviceMount;
	IAgv iAgv;

	@Override
	public AgvMountBean getBean(int agvid) {
		// TODO Auto-generated method stub
		String key=String.format("agvmount_%d", agvid);
		return null;
	}

	@Override
	public void saveCache(int agvid, AgvMountBean bean) {
		// TODO Auto-generated method stub
		String key=String.format("agvmount_%d", agvid);

	}

	@Override
	public void clear(int agvid) {
		// TODO Auto-generated method stub
		String key=String.format("agvmount_%d", agvid);
	}

	
	/**
	 * 双方装货情况的判断
	 */
	@Override
	public void judgement(int agvid, int deviceid) {
		// 拿到对接设备的解析方式
		DeviceDao device = iDevice.getDao(deviceid);
		if (device == null) {
			// 设备不存在
			return;
		}
		DeviceMountBean dmount = iDeviceMount.getBean(deviceid);
		if (dmount == null) {
			// 设备装卸信息不存在
			return;
		}
		if (dmount.isNeedIn() == false && dmount.isNeedOut()) {
			// 对接机台不需要传输
			return;
		}
		AgvMountBean agvMount = getBean(agvid);
		if (agvMount == null) {
			// 小车没有装卸信息
			return;
		}
		IDeviceProtocol deviceEvent = iDevice.getRealDevice(device.getIp());

		// 只上料部分
		if (device.getTransType() == TransDirectionEnum.OnlyIn && dmount.isNeedIn()) {
			if (dmount.getInNum() % 10 != 0) {
				// 数量不足
				return;
			}
			// 小车上层未操作+小车上面有料+设备上层传输
			if (agvMount.isUpHasTrans() == false && device.isUpTrans() && agvMount.isUpFull()) {

				// 需要对接设备上层收货
				byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.In);
				DataSendTools.sendData(device.getIp(), device.getPort(), cmd);

				// 小车上层发货
				iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.UpOut);

				// 传输中，装卸信息锁定
				iDeviceMount.setTransing(deviceid, true);
				return;
			}
			// 小车下层未操作+小车下面有料+设备下层传输
			if (agvMount.isDownHasTrans() == false && device.isDownTrans() && agvMount.isDownFull()) {
				// 需要对接设备下层收货
				byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.In);
				DataSendTools.sendData(device.getIp(), device.getPort(), cmd);

				// 小车下层发货
				iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.DownOut);

				// 传输中，装卸信息锁定
				iDeviceMount.setTransing(deviceid, true);
				return;
			}

			// 如果处理不了
			byte[] goout = deviceEvent.applyLeave(device.getLine(), ApplyTransferEnum.Out);
			DataSendTools.sendData(device.getIp(), device.getPort(), goout);
			return;
		}

		// 只出料
		if (device.getTransType() == TransDirectionEnum.OnlyOut && dmount.isNeedOut()) {
			if (dmount.getOutNum() % 10 != 0) {
				return;
			}

			// 小车上层未操作+小车上面空+设备上层传输
			if (agvMount.isUpHasTrans() == false && device.isUpTrans() && agvMount.isUpFull() == false) {
				// 需要对接设备上层发货
				byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.Out);
				DataSendTools.sendData(device.getIp(), device.getPort(), cmd);

				// 小车上层收货
				iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.UpIn);

				// 传输中，装卸信息锁定
				iDeviceMount.setTransing(deviceid, true);
				return;
			}
			// 小车下层未操作+小车下面空 +设备下层传输
			if (agvMount.isDownHasTrans() == false && device.isDownTrans() && agvMount.isDownFull() == false) {
				// 需要对接设备下层发货
				byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.Out);
				DataSendTools.sendData(device.getIp(), device.getPort(), cmd);

				// 小车下层收货
				iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.DownIn);

				// 传输中，装卸信息锁定
				iDeviceMount.setTransing(deviceid, true);
				return;
			}
			// 如果处理不了
			byte[] goout = deviceEvent.applyLeave(device.getLine(), ApplyTransferEnum.Out);
			DataSendTools.sendData(device.getIp(), device.getPort(), goout);
			return;
		}

		// 同时上下料
		if (device.getTransType() == TransDirectionEnum.Both) {
			// 最好的情况，双方匹配，数量对得上
			if (dmount.isNeedOut() && dmount.isNeedIn() && dmount.getOutNum() > 0 && dmount.getOutNum() % 10 == 0
					&& dmount.getInNum() > 0 && dmount.getInNum() % 10 == 0 && agvMount.isDownHasTrans() == false
					&& agvMount.isUpHasTrans() == false) {

				// 同时传输
				// 先通知对接设备收货
				byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.Both);
				DataSendTools.sendData(device.getIp(), device.getPort(), cmd);
				if (agvMount.isUpFull()) {
					// 上出下入
					iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.UpOutAndDownIn);

				} else {
					// 上入下出
					iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.UpInAndDownOut);
				}
				iDeviceMount.setTransing(deviceid, true);
				return;
			}

			// 小车先收货
			if (dmount.isNeedOut()) {
				if (dmount.getOutNum() > 0 && dmount.getOutNum() % 10 == 0) {
					// 上层空+上层未传输
					if (agvMount.isUpHasTrans() == false && agvMount.isUpFull() == false) {

						// 通知对接机传输发货
						byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.Out);
						DataSendTools.sendData(device.getIp(), device.getPort(), cmd);
						// 小车上层收货
						iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.UpIn);
						iDeviceMount.setTransing(deviceid, true);
						return;
					}
					// 下层空+下层未传输
					if (agvMount.isDownFull() == false && agvMount.isDownHasTrans() == false) {

						// 通知对接机传输发货
						byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.Out);
						DataSendTools.sendData(device.getIp(), device.getPort(), cmd);
						iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.DownIn);
						iDeviceMount.setTransing(deviceid, true);
						return;
					}
					// 已传输过，需要发货
				} else {
					// 数量不足，中断，等待数量
					return;
				}
			}

			// 2.小车再发货
			if (dmount.isNeedIn()) {
				if (dmount.getInNum() > 0 && dmount.getInNum() % 10 == 0) {
					// 上层有料+上层未传输
					if (agvMount.isUpHasTrans() == false && agvMount.isUpFull()) {

						// 通知对接机传输收货
						byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.In);
						DataSendTools.sendData(device.getIp(), device.getPort(), cmd);
						// 小车上层发货
						iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.UpOut);
						iDeviceMount.setTransing(deviceid, true);
						return;
					}
					// 下层有料+下层未传输
					if (agvMount.isDownFull() == false && agvMount.isDownHasTrans()) {

						// 通知对接机传输收货
						byte[] cmd = deviceEvent.applyTransfer(device.getLine(), ApplyTransferEnum.In);
						DataSendTools.sendData(device.getIp(), device.getPort(), cmd);
						iAgv.updateCurrentTrans(agvid, AgvCurrentTransEnum.DownOut);
						iDeviceMount.setTransing(deviceid, true);
						return;
					}
				} else {
					// 数量不足，中断，等待数量
				}
			}

		}

		// 最后申请离开
		ApplyTransferEnum ltype = ApplyTransferEnum.In;
		if (device.getLine() == 1) {
			// 对接设备上料
			ltype = ApplyTransferEnum.In;

		} else if (device.getLine() == 2) {
			// 对接设备下料
			ltype = ApplyTransferEnum.Out;
		} else {
			// 对接设备全料
		}
		// 发送
		byte[] goout = deviceEvent.applyLeave(device.getLine(), ltype);
		DataSendTools.sendData(device.getIp(), device.getPort(), goout);

	}

}
